//
//  RecListenDetailsViewController.m
//  TravelEveryDay
//
//  Created by dlios on 15-10-23.
//  Copyright (c) 2015年 com.lanou3g. All rights reserved.
//

#import "RecListenDetailsViewController.h"

@interface RecListenDetailsViewController ()

@end

@implementation RecListenDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    _myView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    _myView.contentSize = CGSizeMake(0, self.view.frame.size.height * 5);
    [self.view addSubview:_myView];
    _web = [[UIWebView alloc] initWithFrame:CGRectMake(-10 * WIDTH, 260 * HEIGHT, SCREEN_WIDTH - 20 * WIDTH, SCREEN_HEIGHT * 5)];
    _web.scrollView.scrollEnabled = NO;
    [_myView addSubview:_web];
    [_web scalesPageToFit];
    
    [self getData];
    [self setUpPage];
    
    
    NSURL *url = [NSURL URLWithString:self.model.AudioPath];
    //创建AVPlayeritem对象
    _item = [AVPlayerItem playerItemWithURL:url];
    _avPlayer = [AVPlayer playerWithPlayerItem:_item];
}
-(void)setUpPage
{
    //背景图
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200 * HEIGHT)];
    image.userInteractionEnabled = YES;
    [image sd_setImageWithURL:[NSURL URLWithString:self.model.Cover] placeholderImage:PLACEHOLDERIMAGE];
    [_myView addSubview:image];
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(150 * WIDTH, 80 * HEIGHT, 60 * WIDTH, 60 * WIDTH)];
    backView.backgroundColor = [UIColor blackColor];
    backView.alpha = 0.7;
    backView.layer.masksToBounds = YES;
    backView.layer.cornerRadius = 30 * WIDTH;
    [_myView addSubview:backView];
    
    //播放按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"start.png"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateSelected];
    button.center = image.center;
    [button addTarget:self action:@selector(playAudio:) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 60 * WIDTH, 60 * WIDTH);
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 30 * WIDTH;
    [backView addSubview:button];
    
    //作者头像
    UIImageView *iconImage = [[UIImageView alloc] initWithFrame:CGRectMake(10 * WIDTH, 210 * HEIGHT, 40 * WIDTH, 40 * HEIGHT)];
    [iconImage sd_setImageWithURL:[NSURL URLWithString:self.model.EditorPic] placeholderImage:PLACEHOLDERIMAGE];
    iconImage.layer.masksToBounds = YES;
    iconImage.layer.cornerRadius = 20;
    [_myView addSubview:iconImage];
    
    //作者名字
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(60 * WIDTH, 210 * HEIGHT, 100 * WIDTH, 40 * HEIGHT)];
    nameLabel.text  = self.model.EditorName;
    nameLabel.alpha = 0.6;
    [_myView addSubview:nameLabel];
    
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(240 * WIDTH, 210 * HEIGHT, 120 * WIDTH, 40 * HEIGHT)];
    timeLabel.alpha = 0.6;
    timeLabel.textAlignment = NSTextAlignmentCenter;
    //日期转换
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:self.model.CreatTime.integerValue];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"YYYY.MM.dd";
    NSString *str = [formatter stringFromDate:date];
    timeLabel.text = str;
    [_myView addSubview:timeLabel];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(10 * WIDTH, 255 * HEIGHT, SCREEN_WIDTH - 20 * WIDTH, 1)];
    line.backgroundColor = [UIColor lightGrayColor];
    [_myView addSubview:line];
}
#pragma mark - 播放方法
-(void)playAudio:(UIButton *)sender
{
    sender.selected = !sender.selected;
    
    if (sender.selected == YES) {
        [_avPlayer play];
    }else{
        [_avPlayer pause];
    }

}
-(void)getData
{
    [Refresh getdataWithString:[NSString stringWithFormat:@"http://webapi.yilule.com:5580/api/Listen?listenId=%@",self.model.Id] block:^(id block) {
        self.htmlStr = [[block objectForKey:@"Data"]objectForKey:@"Content"];
        [_web loadHTMLString:self.htmlStr baseURL:nil];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
